var periodSelectedConfigPanel = null;

function addPeriod(){
	var descriptionPeriod = $("#addDescriptionPeriod").val();
	var startDatePeriod = $("#addStartPeriod").val();
	var endDatePeriod = $("#addFinishPeriod").val();
	if(descriptionPeriod!="" && startDatePeriod!="" && endDatePeriod!=""){
		postJSON("/enma/period/add/", {"description":descriptionPeriod,"type":"2","startDate":startDatePeriod,"endDate":endDatePeriod}, function(data) {
        	if(data.responseCode == 200){
        		$("#addPeriodSuccess").dialog();
        		getPeriods();
        	}
		});
	}else{
		$("#addPeriodError").dialog();
	}
}

function setPeriodSelectedConfigPanel(period){
	periodSelectedConfigPanel="Periodo: "+period.description + ", Desde: "+period.startDate + ", Hasta: "+period.endDate;;
}

function claseAddPeriodSuccess(){
	$("#addPeriodSuccess").dialog("close");
}

function changePeriodSelected(){
	var selectVal = $("#selectPeriod").val();
	var optionSelected = periods[selectVal];
	setDefaultPeriod(optionSelected);
	$("#startPeriodSelected").val(optionSelected.startDate);
	$("#finishPeriodSelected").val(optionSelected.endDate);
	periodSelectedConfigPanel = "Periodo: "+optionSelected.description + ", Desde: "+optionSelected.startDate + ", Hasta: "+optionSelected.endDate;
}

function getPeriods(){
	showProgress('loadGrl');
	$.getJSON('/enma/period/getNotSpecialPeriods/',function(data) {
		if (data.responseCode == 200) {
			$("#selectPeriod").empty();
			periods = data.periods;
			for(var i = 0; i<periods.length; i++){
				if(i==0){
					$("#startPeriodSelected").val(periods[i].startDate);
					$("#finishPeriodSelected").val(periods[i].endDate);
					periodSelectedConfigPanel = "Periodo: "+periods[i].description + ", Desde: "+periods[i].startDate + ", Hasta: "+periods[i].endDate;
				}
				$("#selectPeriod").append("<option value='"+i+"')>"+periods[i].description+"</option>");
			}
			setDefaultPeriod(periods[0]);
		}
		setTimeout(function() { 
			hideProgress('loadGrl');	    
	    }, 1000); 
		
	});
}